Refer to the following code:
// SPDX-License-Identifier: SOME IDENTIFIER
pragma solidity ^0.8.10;
contract StackContract {
function saveInStack() public pure returns(uint, uint){
uint varFirst = 10;
uint varSecond = 20;
return (varFirst, varSecond);
}
}
In Stack storage, we do not need to specify any keywords to save
the data to Stack as it’s the default choice inside a function to save
the data. Among Storage, Memory, and Stack, the Stack uses the
least gas cost for saving the data, and yet can hold a limited amount
of data. Please note that the variables that pass by reference, i.e.,
Array, Stuct, String, Mappings etc., can be stored either in storage or
in memory and not in stack, which is only meant for storing the data
in small amounts. Also, it’s worth mentioning that while writing error
free code is a mandate in any programming language, it’s always
advisable to write with best practices and attend to all the warnings
for adhering to the coding standards and deliver quality.
2.5.14 Control of Flow in Solidity
Writing some complex algorithm in a smart contract also needs to
execute many different types of loops. Some of the loops that
Solidity supports are the following:
if, else,
while, do,
for,
break,
continue,
return,